package com.clickpaas.timelineofUN.dao.Impl;


import com.bizcloud.ipaas.t7aef1c33e4a445f2833f9d631cb90c38.d20210202091529.codegen.ProjectproviderApi;
import com.bizcloud.ipaas.t7aef1c33e4a445f2833f9d631cb90c38.d20210202091529.model.*;
import com.clickpaas.timelineofUN.bean.TimelineDTOBean;
import com.clickpaas.timelineofUN.dao.ProjectProviderDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Repository
@Slf4j
public class ProjectProviderDaoImpl implements ProjectProviderDao {

    @Resource
    private TimelineDTOBean timelineDtoBean;
    @Resource
    private ProjectproviderApi projectproviderApi;
    /**
     * 根据RegisteredUser查询项目方
     *
     * @param RegisteredUser
     * @return
     */
    @Override
    public List<ProjectProviderDTOResponse> projectproviderByUserId(String RegisteredUser) {
        ProjectProviderDTO providerDTO = timelineDtoBean.projectProviderDTO();
        providerDTO.setRegisteredUser(RegisteredUser);
        return queryProjectProvider(providerDTO);
    }

    /**
     * 根据projectprovider_Name查询项目方
     * @param name
     * @return
     */
    @Override
    public List<ProjectProviderDTOResponse> projectproviderByName(String name) {
        ProjectProviderDTO providerDTO = timelineDtoBean.projectProviderDTO();
        providerDTO.setName(name);
        return queryProjectProvider(providerDTO);

    }

    /**
     * 根据主键查询项目方
     * @param Id
     * @return
     */
    @Override
    public List<ProjectProviderDTOResponse> projectproviderByProviderId(String Id) {
        ProjectProviderDTO providerDTO = timelineDtoBean.projectProviderDTO();
        providerDTO.setId(Id);
        return queryProjectProvider(providerDTO);

    }

    /**
     * 查询项目方数据
     * @param providerDTO
     * @return
     */
    private List<ProjectProviderDTOResponse> queryProjectProvider(ProjectProviderDTO providerDTO){
        DataResponseListProjectProviderDTO providerUsingInfo = projectproviderApi.findProjectProviderUsingPOST(providerDTO);
        if (null == providerUsingInfo
                || null == providerUsingInfo.getData()
                || !"SUCCESS".equals(providerUsingInfo.getMessage())){
            log.info("当前查询的数据：【{}】",providerUsingInfo);
            return null;
        }
        return providerUsingInfo.getData();
    }

    /**
     * 查询所有项目方数据
     *
     * @return
     */
    @Override
    public List<ProjectProviderDTOPageInfoResponse> getProjectProviderAllData() {
        Double total = getTotal();
        if (total != null) {
            int pageSize = 100;
            double v = total / pageSize;
            double ceil = Math.ceil(v);
            long round = Math.round(ceil);

            List<ProjectProviderDTOPageInfoResponse> listAll = new ArrayList<>();
            for (int i = 1; i <= round; i++) {
                DataResponseProjectProviderDTOPageInfodata pageInfodata = getPPByPageInfo(i, pageSize);
                List<ProjectProviderDTOPageInfoResponse> dataList = pageInfodata.getDataList();
                if (null != pageInfodata && dataList.size()>0){
                    listAll.addAll(dataList);
                }
            }
            if (listAll.size() > 0) {
                return listAll;
            }
        }
        return null;
    }

    /**
     * 获取项目数据总数
     * @return
     */
    private Double getTotal(){
        Integer pageNum = 1;
        Integer pageSize = 5;
        DataResponseProjectProviderDTOPageInfodata ppByPageInfo = getPPByPageInfo(pageNum, pageSize);
        if (null != ppByPageInfo
                && StringUtils.isNotBlank(ppByPageInfo.getTotal())){
            return Double.valueOf(ppByPageInfo.getTotal());
        }
        return null;
    }

    /**
     * 分页查询项目方数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    public DataResponseProjectProviderDTOPageInfodata getPPByPageInfo(Integer pageNum, Integer pageSize) {
        ProjectProviderDTOPageInfo pPInfoData = timelineDtoBean.projectProviderDTOPageInfo();
        pPInfoData.set$PageNum(pageNum);
        pPInfoData.set$PageSize(pageSize);
        DataResponseProjectProviderDTOPageInfo postPPData = projectproviderApi.findPageInfoProjectProviderUsingPOST(pPInfoData);
        if (null == postPPData || null == postPPData.getData())return null;
        return postPPData.getData();
    }
}
